package com.zhixun.mobile.modules.ads.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.wechat.entity.TimingScore;
import com.boxin.ims.modules.wechat.entity.TimingScoreLog;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.TimingScoreLogService;
import com.boxin.ims.modules.wechat.service.TimingScoreService;
import com.boxin.ims.modules.wechat.service.WechatMemberService;
import com.boxin.ims.modules.wechat.utils.ScoreUtils;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.zhixun.mobile.modules.base.web.BaseController;


@Controller
@RequestMapping(value = "/tscore")
public class TimingScoreFrontController  extends BaseController{
	
	
	@Autowired
	private TimingScoreService timingScoreService;
	
	@Autowired
	private TimingScoreLogService timingScoreLogService;
	
	@Autowired
	private WechatMemberService wechatMemberService;
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-5-8  下午11:43:06
	 * @function <p> 导航到 登录提交页 </p>
	 * @param id
	 * @param uid
	 * @param request
	 * @param response
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "totscore")
	public String totscore(Long id,Long uid,HttpServletRequest request,	HttpServletResponse response, Model model,	RedirectAttributes redirectAttributes) {
		
		TimingScore timingScore = timingScoreService.get(id);
		
		model.addAttribute("id", id);
		model.addAttribute("uid", uid);
		model.addAttribute("timingScore", timingScore);
		
		return "/modules/advert/tscoreView";
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-5-8  下午11:41:36
	 * @function <p> 扫描 后提交的处理 </p>
	 * @param id
	 * @param uid
	 * @param request
	 * @param response
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "dotscore")
	public String dotscore(Long id,Long uid,String mobile,String pwd,HttpServletRequest request,	HttpServletResponse response, Model model,	RedirectAttributes redirectAttributes) {
		
		User user = new User();
		user.setId(uid);
		WechatMember member = wechatMemberService.getByMobile(user, mobile);
		if(member != null){
			
			
			TimingScore timingScore  = timingScoreService.get(id);
			if(timingScore!=null){
				if(timingScore.isStartup()){
					
					Date startTime = timingScore.getStartTime();
					Date endTime   = timingScore.getEndTime();
					Date nowTime  = new Date();
					
					String start = DateUtils.formatDate(startTime, "HHmmss");
					String end   = DateUtils.formatDate(endTime, "HHmmss");
					String now   = DateUtils.formatDate(nowTime, "HHmmss");
					
					int iStart = Integer.parseInt(start);
					int iEnd   = Integer.parseInt(end);
					int iNow   = Integer.parseInt(now);
					
					//在时间范围内
					if(iNow >iStart  && iNow < iEnd){
						//1.判断该会员是否今天参与过了
						
						TimingScoreLog slog  = new TimingScoreLog();
						slog.setDay(DateUtils.getTodayString());
						slog.setMember(member);
						slog.setTimingScore(timingScore);
						
						int qCount = timingScoreLogService.findCount(slog);
						
						//已经扫描获得积分
						if(qCount ==0 ){
							//2.加积分
							int addScore = timingScore.getScore();
							ScoreUtils.addScore(member, addScore,"0","定时定点扫描送"+addScore/1000d+"个活动积分");
							
							String message = "亲，扫描成功,本次送您"+addScore/1000d+"个活动积分!";
							
							timingScoreLogService.saveFromTimeScore(timingScore, member);
							
							request.setAttribute("message", message);
//							addMessage(redirectAttributes, message);
						}else{
							//未扫找
							String message = "亲，对不起,系统未能找到应的活动信息,处理失败!";
							request.setAttribute("message", message);
//							addMessage(redirectAttributes, message);
							return "/modules/advert/tscoreError";
						}
						
					}else{
						String message = "本活动仅在 " + DateUtils.formatDate(startTime, "HH时mm分ss秒") +"至 " + DateUtils.formatDate(endTime, "HH时mm分ss秒 区间段") +"有效！";
						System.out.println(message);
						request.setAttribute("message", message);
//						addMessage(redirectAttributes, message);
						return "/modules/advert/tscoreError";
					}
					
				}
				
				String message = "亲，本活动还没有开始呢，谢谢您的关注，请关注微信公众平台的公告消息！";
//				addMessage(redirectAttributes, message);
				request.setAttribute("message", message);
				model.addAttribute("timingScore", timingScore);
			}
		}
		
		model.addAttribute("id", id);
		model.addAttribute("uid", uid);
		
		return "/modules/advert/tscoreOK";
	}
	
	
	
	
	

}
